package gui.addon;

import java.awt.Color;
import java.awt.Graphics;

public class Processbar {
	private long total = 0;
	
	private int height = 0;
	private int width = 0;
	
	private String text;

	private final int SQUARE_WIDTH = 20;
	private final int SQUARE_HEIGHT = 40;
	private final int SQUARE_PADDING = 10;
	private final int SQUARE_TOTAL = 10;
	private final int TEXT_OFFSET = 10;
	private final int SQUARE_LENGTH = (SQUARE_WIDTH + SQUARE_PADDING) * SQUARE_TOTAL;
	private final int TOTAL_HEIGHT = SQUARE_HEIGHT + 4*TEXT_OFFSET;
	
	public Processbar(int width, int height, String text) {
		this.height = height;
		this.width = width;
		this.text = text;
	}
	/**
	 * Drawing loading bar
	 * @param g
	 */
	public void draw(Graphics g, long curr) {
		if (total == 0) 
			return;
		
		float percent = (float) curr / total;

		// How many bars!
		int currSquares = (int) Math.round(SQUARE_TOTAL * percent);

		g.setColor(Color.black);
		g.fillRect(width/2 - (SQUARE_LENGTH + 20)/2, height/2 - (TOTAL_HEIGHT + 20)/2, SQUARE_LENGTH + 10, TOTAL_HEIGHT);
		
		for (int i = 0; i < SQUARE_TOTAL; i++) {
			if (i < currSquares)
				g.setColor(Color.red);
			else
				g.setColor(Color.gray);
			
			// Filling the bar
			g.fillRect((i * (SQUARE_WIDTH + SQUARE_PADDING)) + width/2 - SQUARE_LENGTH/2, height/2 - SQUARE_HEIGHT/2, SQUARE_WIDTH,
					SQUARE_HEIGHT);
		}
		

		// Loading message
		String loading = String.format("%s %.2f%s",text, percent*100,"%");
		g.setColor(Color.white);
		g.drawString(loading, width/2 - SQUARE_LENGTH / 2,
				height/2 - SQUARE_HEIGHT/2 - TEXT_OFFSET);
		System.out.println(loading);
	}
	
	public void setTotal(long total) {
		this.total = total;
	}
	
	public long getTotal() {
		return total;
	}
	public void setText(String text) {
		this.text = text;
	}
}
